package cn.stylefeng.guns.user.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.core.annotion.BusinessLog;
import cn.stylefeng.guns.core.enums.LogAnnotionOpTypeEnum;
import cn.stylefeng.guns.core.pojo.response.ErrorResponseData;
import cn.stylefeng.guns.core.pojo.response.ResponseData;
import cn.stylefeng.guns.core.pojo.response.SuccessResponseData;
import cn.stylefeng.guns.sys.modular.auth.service.AuthService;
import cn.stylefeng.guns.user.entity.User;
import cn.stylefeng.guns.user.dto.UserDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import cn.stylefeng.guns.user.service.UserService;
import cn.stylefeng.guns.user.model.param.UserParam;

import javax.annotation.Resource;

/**
 * user 控制器
 *
 * @author: aria
 * @date: 2021/02/20 20:24
 */
@Api(tags = "用户相关")
@RestController
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private AuthService authService;


    /**
     * 登录
     */
    @ApiOperation("用户登录")
    @GetMapping("/user/load")
    @BusinessLog(title = "用户登录", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData load(String userName, String password) {

        //登录并返回token
        return new SuccessResponseData(userService.Load(userName, password));
    }

    /**
     * 测试日志文件
     */
    @ApiOperation("测试日志文件")
    @PostMapping("/user/testLog")
    @BusinessLog(title = "测试日志文件", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData testLog() {
        int i = 0 / 1;
        User user = null;
        System.out.println(user.getSex());
        return new SuccessResponseData(i);
    }


    /**
     * 获取全部user
     *
     * @author aria
     * @date 2021/02/20 20:24
     */
    @ApiOperation("user_查询所有")
    @GetMapping("/user/list")
    @BusinessLog(title = "user_查询所有", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData list() {
        return new SuccessResponseData(userService.list());
    }

    /**
     * 查看详情user
     *
     * @author aria
     * @date 2021/02/20 20:24
     */

    @ApiOperation("只需要传id,根据id查用户")
    @GetMapping("/user/detail")
    @BusinessLog(title = "user_查看详情", opType = LogAnnotionOpTypeEnum.DETAIL)
    public ResponseData detail(@Validated(UserParam.detail.class) UserParam userParam) {
        UserDTO userDTO = new UserDTO();
        BeanUtil.copyProperties(userParam, userDTO);
        return new SuccessResponseData(userService.detail(userDTO));
    }

    /**
     * 添加user
     *
     * @author aria
     * @date 2021/02/20 20:24
     */
    @ApiOperation("用户注册")
    @PostMapping("/user/add")
    @BusinessLog(title = "user_增加", opType = LogAnnotionOpTypeEnum.ADD)
    public ResponseData add(@RequestBody @Validated(UserParam.add.class) UserParam userParam) {
        UserDTO userDTO = new UserDTO();
        BeanUtil.copyProperties(userParam, userDTO);
        User add = userService.add(userDTO);
        if (add == null) {
            return new ErrorResponseData(500, "该用户名已被使用");
        }
        return new SuccessResponseData(add);
    }

    /**
     * 删除user
     *
     * @author aria
     * @date 2021/02/20 20:24
     */
    @ApiOperation("只需要传id,根据id删")
    @PostMapping("/user/delete")
    @BusinessLog(title = "user_删除", opType = LogAnnotionOpTypeEnum.DELETE)
    public ResponseData delete(@RequestBody @Validated(UserParam.delete.class) UserParam userParam) {
        UserDTO userDTO = new UserDTO();
        BeanUtil.copyProperties(userParam, userDTO);
        userService.delete(userDTO);
        return new SuccessResponseData();
    }

    /**
     * 编辑user
     *
     * @author aria
     * @date 2021/02/20 20:24
     */
    @ApiOperation("更新用户基本信息,想更新什么数据就传什么数据")
    @PostMapping("/user/edit")
    @BusinessLog(title = "user_编辑", opType = LogAnnotionOpTypeEnum.EDIT)
    public ResponseData edit(@RequestBody @Validated(UserParam.edit.class) UserParam userParam) {
        UserDTO userDTO = new UserDTO();
        BeanUtil.copyProperties(userParam, userDTO);
        userService.edit(userDTO);
        return new SuccessResponseData();
    }

    /**
     * 分页查询user
     *
     * @author aria
     * @date 2021/02/20 20:24
     */
    @GetMapping("/user/page")
    @BusinessLog(title = "user_分页查询", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData page(UserParam userParam) {
        UserDTO userDTO = new UserDTO();
        BeanUtil.copyProperties(userParam, userDTO);
        return new SuccessResponseData(userService.page(userDTO));
    }

    /**
     * 登录
     */
    @ApiOperation("管理员用户登录")
    @PostMapping("/user/adminLogin")
    @BusinessLog(title = "管理员登录", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData adminLogin(@RequestBody UserParam userParam) {
        UserDTO userDTO = new UserDTO();
        BeanUtil.copyProperties(userParam, userDTO);
        return userService.adminLogin(userDTO);

    }
}